package com.njzxw.fs.commonutil.util;

import org.springframework.stereotype.Service;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.SecureRandom;

@Service
public class VideoPartUtil {

    Key key;
    /**
     * 根据参数生成KEY
     */
    public void getKey(String strKey) {
        try {
            KeyGenerator _generator = KeyGenerator.getInstance("DES");
            _generator.init(new SecureRandom(strKey.getBytes()));
            this.key = _generator.generateKey();
            _generator = null;
        } catch (Exception e) {
            throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
        }
    }

    /**
     * 文件file进行加密并保存目标文件destFile中
     *
     * @param file   要加密的文件 如c:/test/srcFile.txt
     * @param destFile 加密后存放的文件名 如c:/加密后文件.txt
     */
    public void encrypt(String key,String file, String destFile) throws Exception {
        InputStream is = null;
        OutputStream out = null;
        try{

            getKey(key);

            Cipher cipher = Cipher.getInstance("DES");
            // cipher.init(Cipher.ENCRYPT_MODE, getKey());
            cipher.init(Cipher.ENCRYPT_MODE, this.key);
            is = new FileInputStream(file);
            byte[] buffer1 = new byte[2048000];
            out = new FileOutputStream(destFile);
            out.write(buffer1, 0, is.read(buffer1));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(is != null){
                is.close();
            }
            if(out != null){
                out.close();
            }
        }
    }

    /**
     * 文件采用DES算法解密文件
     *
     * @param file 已加密的文件 如c:/加密后文件.txt
     *         * @param destFile
     *         解密后存放的文件名 如c:/ test/解密后文件.txt
     */
    public void decrypt(String key,String file, String dest) throws Exception {
        InputStream is = null;
        OutputStream out = null;
        CipherOutputStream cos = null;
        try {

            getKey(key);

            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, this.key);
            is = new FileInputStream(file);
            out = new FileOutputStream(dest);
            cos = new CipherOutputStream(out, cipher);
            byte[] buffer = new byte[1024];
            int r;
            while ((r = is.read(buffer)) >= 0) {
                cos.write(buffer, 0, r);
            }
        }finally {
            if(cos != null){
                cos.close();
            }
            if(out != null){
                out.close();
            }
            if(is != null){
                is.close();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        VideoPartUtil td = new VideoPartUtil();
        td.encrypt("aa","/Users/eliayng/Pictures/视频/1049_M0118400000Ypm1V1G0U5J1001587978.f40.mp4", "/Users/eliayng/Pictures/视频/cesces测试1.mp4"); //加密
        td.decrypt("aa","/Users/eliayng/Pictures/视频/cesces测试1.mp4", "/Users/eliayng/Pictures/视频/cesces测试2.mp4"); //解密

    }

}
